//==============================================================================
//
// Title:       BscanTestSetDio.c
// Purpose:     A short description of the implementation.
//
// Created on:  2012-7-13 at 10:35:35 by luh.
// Copyright:   dafda. All Rights Reserved.
//
// ************************* 78 character ruler *****************************

#include "RFBB.h"         
#include <utility.h>
#include <formatio.h> 
#include "USB6501.h"
                  

_DAQMX_WRITE_PORT_CMD g_szBscanWritePort2Cmd[2] = {{"BSCAN_SHORT","DIO1/port2",BSCANSHORTCHAIN},      
												 {"BSCAN_LONG","DIO1/port2",BSCANLONGCHAIN}};    

                                                 

/**************************************************************************************************
  Function:          BscanSetShortChainStatus
  Description:      Control bscan pin on lmp board.
  Calls:            none
  Called By:        dll
  Table Accessed:   none
  Table Updated:    none
  Input:            none
  Output:           error code
  Return:           none
  Test Case ID:     none
  Used Command:     g_szBscanWritePort2Cmd
**************************************************************************************************/

void __decldllexp BscanSetShortChainStatus(tTestData *testData, tTestError *testError)
{
    ErrMsg errMsg = {'\0'};
    ERRORINFO errorInfo;   
    ErrMsg szLogMsg;
    CAObjHandle hStepName = 0;
    char* pszStepName = NULL;
    char kszShmonAgent[BUFFER_SIZE_256] ={'\0'};

    int  error = 0;
    int  iPassFail = 1;

    //////////////////////////////////////////////////////////////////////////   
    // Resolve StepName 
    tsErrChk(TS_SeqContextGetProperty (testData->seqContextCVI, &errorInfo, 
                                       TS_SeqContextStep, CAVT_OBJHANDLE, 
                                       &hStepName));
	tsErrChk(TS_StepGetProperty (hStepName, &errorInfo, TS_StepName, CAVT_CSTRING, 
                                 &pszStepName));
	sprintf( szLogMsg, TEST_NAME_EVENT_FMTSTR, pszStepName );
    ErrChk( EELog_INEventLogLogEvent( g_hEventLog, NULL, szLogMsg ) );
	
	// Post message to GUI
    tsErrChk(TS_ThreadPostUIMessageEx (g_hThreadPostUIMessage, NULL,
                                       TS_UIMsg_ProgressText, 0.0,
                                       pszStepName, 0, VTRUE));
	

    //--------------------------------------------------------------------
    sprintf( szLogMsg, "##### Bscan Set Short Chain Status to flash#####" );
    ErrChk(EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg));
       
    // Bscan Set to ShortChain
    ErrChk(USBWriteDigToPort(g_szBscanWritePort2Cmd[0].ucTashName,g_szBscanWritePort2Cmd[0].ucDevport,g_szBscanWritePort2Cmd[0].uiWriteData));
    Delay(2.0);


    
Error:
    
#if(ON_OFF != SIMU_VERS) 
    
    if(iPassFail == 1)
    	{
        testData->result = PASS;
    	}
    else 
    	{
        testData->result = FAIL;
    	}

    // FREE RESOURCES
    CA_DISCARD_OBJ( hStepName );
    CA_FREE( pszStepName );

    // If an error occurred, set the error flag to cause a run-time error in TestStand.
    if(error < 0)
    	{
        testError->errorFlag = TRUE;
        testError->errorCode = error;
        testData->replaceStringFuncPtr(&testError->errorMessage, errMsg);
    	}
#else
    testData->result = PASS;
#endif

   // Clear message from GUI
   TS_ThreadPostUIMessageEx (g_hThreadPostUIMessage, NULL,
                                 TS_UIMsg_ProgressText, 0.0, " ", 0, VTRUE);

   return;    
}



/**************************************************************************************************
  Function:          BscanSetLongChainStatus
  Description:      Control bscan pin on lmp board.
  Calls:            none
  Called By:        dll
  Table Accessed:   none
  Table Updated:    none
  Input:            none
  Output:           error code
  Return:           none
  Test Case ID:     none
  Used Command:     g_szSFPWritePort2Cmd
**************************************************************************************************/

void __decldllexp BscanSetLongChainStatus(tTestData *testData, tTestError *testError)
{
    ErrMsg errMsg = {'\0'};
    ERRORINFO errorInfo;   
    ErrMsg szLogMsg;
    CAObjHandle hStepName = 0;
    char* pszStepName = NULL;

    int  error = 0;
    int  iPassFail = 1;

    //////////////////////////////////////////////////////////////////////////   
    // Resolve StepName 
    tsErrChk(TS_SeqContextGetProperty (testData->seqContextCVI, &errorInfo, 
                                       TS_SeqContextStep, CAVT_OBJHANDLE, 
                                       &hStepName));
    tsErrChk(TS_StepGetProperty (hStepName, &errorInfo, TS_StepName, CAVT_CSTRING, 
                                 &pszStepName));

    sprintf( szLogMsg, TEST_NAME_EVENT_FMTSTR, pszStepName );
    ErrChk( EELog_INEventLogLogEvent( g_hEventLog, NULL, szLogMsg ) );

    // Post message to GUI
    tsErrChk(TS_ThreadPostUIMessageEx (g_hThreadPostUIMessage, NULL,
                                       TS_UIMsg_ProgressText, 0.0,
                                       pszStepName, 0, VTRUE));

    //--------------------------------------------------------------------
    sprintf( szLogMsg, "##### Bscan Set Long Chain Status to test#####" );
    ErrChk(EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg));
       
    // Bscan Set to LongChain
    ErrChk(USBWriteDigToPort(g_szBscanWritePort2Cmd[1].ucTashName,g_szBscanWritePort2Cmd[1].ucDevport,g_szBscanWritePort2Cmd[1].uiWriteData));
    Delay(2.0);

    
Error:
    
#if(ON_OFF != SIMU_VERS) 
    
    if(iPassFail == 1)
    	{
        testData->result = PASS;
    	}
    else 
    	{
        testData->result = FAIL;
    	}

    // FREE RESOURCES
    CA_DISCARD_OBJ( hStepName );
    CA_FREE( pszStepName );

    // If an error occurred, set the error flag to cause a run-time error in TestStand.
    if(error < 0)
    	{
        testError->errorFlag = TRUE;
        testError->errorCode = error;
        testData->replaceStringFuncPtr(&testError->errorMessage, errMsg);
    	}
#else
    testData->result = PASS;
#endif

   // Clear message from GUI
   TS_ThreadPostUIMessageEx (g_hThreadPostUIMessage, NULL,
                                 TS_UIMsg_ProgressText, 0.0, " ", 0, VTRUE);

   return;    
}

// ************************* 78 character ruler *****************************

